<?php

namespace App\Http\Controllers\Home;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\ShopSettings;
use App\Products;
use App\ShopCategories;

class ShopController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        // $this->middleware('auth');
    }

    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Http\Response
     */
    public function index($shopId, $cateId=null)
    {
        $shopSettings = ShopSettings::where('shop_id', $shopId)->first();
        if(!$shopSettings) {
            abort('404');
        }

        //商品展示
        $products = Products::where('shop_id', $shopId);

        //所有分类
        $shopCategories = ShopCategories::where([
            'shop_id' => $shopId,
            'parent_id' => 0,
            // 'deleted_at' => null,
        ])->get();

        //当前分类的子分类
        $shopCateCurrent = null;
        if ((int)$cateId != null) {
            $shopCateCurrent = ShopCategories::find($cateId)->hasChildren;
            $products = $products->where('shop_category_id', $cateId);
        }

        $products = $products->get();

        return view('home.shop_index', [
            'shopSettings' => $shopSettings,
            'shopCategories' => $shopCategories,
            'shopCateCurrent' => $shopCateCurrent,
            'products' => $products,
        ]);
    }

    public function productDetail($productId = null)
    {
        return view('home.product_detail');
    }

    public function create()
    {
        return view('admin.shop_base_settings');
    }

    public function edit($value='')
    {
        # code...
    }
    public function setBases($shop='')
    {
        
    }
}
